<template>
  <el-form
    :model="dynamicValidateForm"
    ref="dynamicValidateForm"
    label-width="100px"
    class="demo-dynamic"
  >
    <el-form-item prop="CommodSpeciName" label="名称">
      <el-input v-model="dynamicValidateForm.CommodSpeciName"></el-input>
    </el-form-item>
    <el-form-item
      v-for="(domain, index) in dynamicValidateForm.domains"
      :label="'规格' + index"
      :key="domain.key"
      :prop="'domains.' + index + '.value'"
      :rules="{
        required: true,
        message: '规格不能为空',
        trigger: 'blur',
      }"
    >
      <el-input v-model="domain.value"></el-input
      ><el-button @click.prevent="removeDomain(domain)">删除</el-button>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('dynamicValidateForm')"
        >提交</el-button
      >
      <el-button @click="addDomain">新增域名</el-button>
      <el-button @click="resetForm('dynamicValidateForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
export default {
  data() {
    return {
      dynamicValidateForm: {
        SmallName: [],
        domains: [{
            value: "",}],
        CommodSpeciName:"",
        
        email: "",
        
      },
      element:[],
      sku: {
          CommodSpeciName: "",
          CommodSmallName: [],
          CommoditySalePrice: "",
        },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert("submit!");
          console.log(this.sku);
          this.sku.CommodSpeciName = this.dynamicValidateForm.CommodSpeciName
          for (let index = 0; index < this.dynamicValidateForm.domains.length; index++){
              this.element.push(this.dynamicValidateForm.domains[index].value)
          }
          this.sku.CommodSmallName = JSON.stringify(this.element)
          this.axios
            .post("https://localhost:44338/api/app/sku/sku",this.sku)
            .then((response) => {});
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    removeDomain(item) {
      var index = this.dynamicValidateForm.domains.indexOf(item);
      if (index !== -1) {
        this.dynamicValidateForm.domains.splice(index, 1);
      }
    },
    addDomain() {
      this.dynamicValidateForm.domains.push({
        value: "",
        //key: Date.now(),
      });
    },
  },
};
</script>
